//
//  YDPositionModel.m
//  PeiZiWangBei
//
//  Created by LK-IOS on 2017/11/28.
//  Copyright © 2017年 LK-IOS. All rights reserved.
//

#import "YDPositionModel.h"

@implementation YDPositionModel

+ (NSDictionary *)mj_replacedKeyFromPropertyName
{
    return @{
             @"Id" : @"id"
             };
}
//年月日时分秒以后台状态改变的时间为准
-(NSString *)buyYearStr
{
    NSString *time;
    if (!self.buyingTime) {
        time = self.createTime;
    }else{
        time = self.buyingTime;
    }
    NSDateFormatter *formate = [[NSDateFormatter alloc] init];
    [formate setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *date = [formate dateFromString:time];
    
    NSDateFormatter *formate1 = [[NSDateFormatter alloc] init];
    [formate1 setDateFormat:@"yyyy.MM.dd"];
    
    return [formate1 stringFromDate:date];
}

-(NSString *)buyHourStr
{
    NSString *time;
    if (!self.buyingTime) {
        time = self.createTime;
    }else{
        time = self.buyingTime;
    }
    NSDateFormatter *formate = [[NSDateFormatter alloc] init];
    [formate setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *date = [formate dateFromString:time];
    
    NSDateFormatter *formate1 = [[NSDateFormatter alloc] init];
    [formate1 setDateFormat:@"HH:mm:ss"];
    
    return [formate1 stringFromDate:date];
}


-(NSString *)saleYearStr
{
    NSDateFormatter *formate = [[NSDateFormatter alloc] init];
    [formate setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *date = [formate dateFromString:self.sellingTime];
    if ([self.state isEqualToString:@"REVOKE"]) {
        date = [formate dateFromString:self.createTime];
    }else{
        date = [formate dateFromString:self.sellingTime];
    }
    
    
    NSDateFormatter *formate1 = [[NSDateFormatter alloc] init];
    [formate1 setDateFormat:@"yyyy.MM.dd"];
    
    return [formate1 stringFromDate:date];
}

-(NSString *)saleHourStr
{
    NSDateFormatter *formate = [[NSDateFormatter alloc] init];
    [formate setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *date = [formate dateFromString:self.sellingTime];
    if ([self.state isEqualToString:@"REVOKE"]) {
        date = [formate dateFromString:self.createTime];
    }else{
        date = [formate dateFromString:self.sellingTime];
    }
    
    NSDateFormatter *formate1 = [[NSDateFormatter alloc] init];
    [formate1 setDateFormat:@"HH:mm:ss"];
    
    return [formate1 stringFromDate:date];
}

-(BOOL)isCanSale
{
    if ([self.state isEqualToString:@"HOLDPOSITION"]) {
        NSDateFormatter *formate = [[NSDateFormatter alloc] init];
        formate.dateFormat = @"yyyy-MM-dd HH:mm:ss";
        NSDate *date = [formate dateFromString:[NSString stringWithFormat:@"%@ %@" , self.buyYearStr , self.buyHourStr]];
        //        NSString *begin = [formate stringFromDate:date];
        if (date.isToday) {
            return NO;
        }else{//判断是不是交易时间段
            if ([NSDate stockTradingTimeJudgment]) {
                return YES;
            }else{
                return NO;
            }
            
        }
        
    }else{
        return NO;
    }
}
-(NSString *)stateString
{
    if ([self.state isEqualToString:@"HOLDPOSITION"]) {
        return @"cc-chicangzhong";
    }else if ([self.state isEqualToString:@"UNKONWN"]){
        return @"cc-weizhi";
    }else if ([self.state isEqualToString:@"POSTED"]){
        return @"cc-mairuzhong";
    }else if ([self.state isEqualToString:@"BUYLOCK"]){
        return @"cc-mairusuoding";
    }else if ([self.state isEqualToString:@"SELLLOCK"]){
        return @"cc-maichusuoding";
    }else if ([self.state isEqualToString:@"UNWIND"]){
        return @"cc-yipingcang";
    }else if ([self.state isEqualToString:@"SELLAPPLY"]){
        return @"cc-maichushenqing";
    }else{
        return @"cc-weizhi";
    }
}
-(BOOL)isSuccess
{
    if ([self.state isEqualToString:@"REVOKE"] ) {
        return NO;
    }else{
        return YES;
    }
}
-(NSString *)windControlString
{
    if ([self.state isEqualToString:@"REVOKE"] ) {
        if (self.windControlType.length) {
            return @"卖出失败";
        }else{
            return @"买入失败";
        }
    }else{
        if ([self.windControlType isEqualToString:@"TRADINGEND"]) {
            return @"交易期满";
        }else if ([self.windControlType isEqualToString:@"REACHPROFITPOINT"]){
            return @"达到止盈点";
        }else if ([self.windControlType isEqualToString:@"REACHLOSSPOINT"]){
            return @"达到止损点";
        }else if ([self.windControlType isEqualToString:@"PUBLISHERAPPLY"]){
            return @"客户申请平仓";
        }else{
            return @"未知";
        }
    }
    
    
}
-(NSString *)pzNumberOfStrand
{
    if (!self.numberOfStrand) {
        return @"--";
    }else{
        CGFloat number = [self.numberOfStrand floatValue];
        if (number < 10000.f) {
            return [NSString stringWithFormat:@"%@股" , [NSString countNumAndChangeformat:[NSString stringWithFormat:@"%@",self.numberOfStrand]]];
        }else{
            //转换为万股
            CGFloat Snumber = number / 10000.f;
            return [NSString stringWithFormat:@"%@万股" , [NSString countNumAndChangeformat:[NSString stringWithFormat:@"%.2f",Snumber]]];
        }
        
    }
}
-(NSString *)pzBuyingPrice
{
    if (!self.buyingPrice) {
        return @"--";
    }else{
        return [self.buyingPrice pz_stringNumberByMultiplyingBy:@"1"];
    }
}
-(NSString *)pzCurrentPrice
{
    if (!self.lastPrice) {
        return @"--";
    }else{
        return [self.lastPrice pz_stringNumberByMultiplyingBy:@"1"];
    }
}
-(NSString *)pzSellingPrice
{
    if (!self.sellingPrice) {
        return @"--";
    }else{
        return [self.sellingPrice pz_stringNumberByMultiplyingBy:@"1"];
    }
}
-(NSString *)lossNumber
{
    if (!self.applyAmount || !self.lossPoint) {
        return @"--";
    }else{
        //计算一个总额度
        NSString *total = [self.applyAmount pz_stringNumberByMultiplyingBy:self.lossPoint];
        NSString *totalNumber = [total pz_stringNumberByMultiplyingBy:@"1.0"];
        CGFloat totalF = [totalNumber floatValue];
        if (totalF < 10000.f) {
            return [NSString stringWithFormat:@"%@元",[NSString countNumAndChangeformat:[NSString stringWithFormat:@"%0.2f",totalF]]];
        }else {//大于一万
            //转换为万
            CGFloat wtotalF = totalF / 10000.f;
            return [NSString stringWithFormat:@"%@万",[NSString countNumAndChangeformat:[NSString stringWithFormat:@"%0.2f",wtotalF]]];
            
        }
    }
    
}
-(NSString *)pzProfitOrLoss
{
    if (!self.profitOrLoss) {
        return @"--";
    }else{
        CGFloat numberF;
        if ([self.profitOrLoss floatValue] <= 0) {
            numberF = [[self.profitOrLoss pz_stringNumberByMultiplyingBy:@"-1.0"] floatValue];
        }else{
            numberF = [self.profitOrLoss floatValue];
        }
        
        if (numberF < 10000.f) {
            NSString *result = [self.profitOrLoss floatValue] <= 0 ? [[NSString stringWithFormat:@"%0.2f",numberF] pz_stringNumberByMultiplyingBy:@"-1.0"] : [NSString stringWithFormat:@"%0.2f",numberF];
            return [NSString stringWithFormat:@"%@元",[NSString countNumAndChangeformat:result]];
        }else{//万
            //转换为万
            CGFloat wtotalF = numberF / 10000.f;
            NSString *result = [self.profitOrLoss floatValue] <= 0 ? [[NSString stringWithFormat:@"%0.2f",wtotalF] pz_stringNumberByMultiplyingBy:@"-1.0"] : [NSString stringWithFormat:@"%0.2f",wtotalF];
            return [NSString stringWithFormat:@"%@万",[NSString countNumAndChangeformat:result]];
        }
    }
}
-(NSString *)pzPublisherProfitOrLoss
{
    if (!self.publisherProfitOrLoss) {
        return @"--";
    }else{
        CGFloat numberF;
        if ([self.publisherProfitOrLoss floatValue] <= 0) {
            numberF = [[self.publisherProfitOrLoss pz_stringNumberByMultiplyingBy:@"-1.0"] floatValue];
        }else{
            numberF = [self.publisherProfitOrLoss floatValue];
        }
        
        if (numberF < 10000.f) {
            NSString *result = [self.publisherProfitOrLoss floatValue] <= 0 ? [[NSString stringWithFormat:@"%0.2f",numberF] pz_stringNumberByMultiplyingBy:@"-1.0"] : [NSString stringWithFormat:@"%0.2f",numberF];
            return [NSString stringWithFormat:@"%@元" , [NSString countNumAndChangeformat:[NSString stringWithFormat:@"%@",result]]];
        }else{//万
            //转换为万
            CGFloat wtotalF = numberF / 10000.f;
            
            NSString *result = [self.publisherProfitOrLoss floatValue] <= 0 ? [[NSString stringWithFormat:@"%0.2f",wtotalF] pz_stringNumberByMultiplyingBy:@"-1.0"] : [NSString stringWithFormat:@"%0.2f",wtotalF];
            
            return [NSString stringWithFormat:@"%@万",[NSString countNumAndChangeformat:[NSString stringWithFormat:@"%@",result]]];
        }
    }
}
-(NSString *)pzReserveFund
{
    if (!self.reserveFund) {
        return @"--";
    }else{
        //计算一个总额度
        CGFloat totalF = [self.reserveFund floatValue];
        if (totalF < 10000.f) {
            return [NSString stringWithFormat:@"%@元" , [NSString countNumAndChangeformat:[NSString stringWithFormat:@"%0.2f",totalF]]];
        }else{//大于万
            //转换为万
            CGFloat wtotalF = totalF / 10000.f;
            return [NSString stringWithFormat:@"%@万",[NSString countNumAndChangeformat:[NSString stringWithFormat:@"%0.2f",wtotalF]]];
        }
    }
}
-(NSString *)pzServiceFee
{
    if (!self.serviceFee) {
        return @"--";
    }else{
        //计算一个总额度
        CGFloat totalF = [self.serviceFee floatValue];
        if (totalF < 10000.f) {
            return [NSString stringWithFormat:@"%@元" , [NSString countNumAndChangeformat:[NSString stringWithFormat:@"%0.2f",totalF]]];
        }else{//大于万
            //转换为万
            CGFloat wtotalF = totalF / 10000.f;
            return [NSString stringWithFormat:@"%@万",[NSString countNumAndChangeformat:[NSString stringWithFormat:@"%0.2f",wtotalF]]];
        }
    }
}
-(NSString *)detailDeduction
{
    if (!self.profitOrLoss || [self.profitOrLoss floatValue]>0) {
        return @"0.0元";
    }else{
        return self.pzProfitOrLoss;
    }
}
-(NSString *)detailThaw
{
    //解冻金额为冻结保证金（本金）减去亏损
    if (!self.profitOrLoss || [self.profitOrLoss floatValue]>0.0) {
        return self.pzReserveFund;
    }else{
        //获取保证金及亏损
        //        NSString *Bond = [NSString stringWithFormat:@"%@" , self.reserveFund];
        //        NSString *Loss = [NSString stringWithFormat:@"%@" , self.profitOrLoss];
        //        NSString *Thaw = [Bond pz_stringNumberByAdding:Loss];
        NSString *Thaw = [NSString stringWithFormat:@"%@" , self.reserveFund];
        //计算一个总额度
        CGFloat totalF = [Thaw floatValue];
        if (totalF < 10000.f) {
            return [NSString stringWithFormat:@"%@元" , [NSString countNumAndChangeformat:[NSString stringWithFormat:@"%0.2f",totalF]]];
        }else{//大于万
            //转换为万
            CGFloat wtotalF = totalF / 10000.f;
            return [NSString stringWithFormat:@"%@万",[NSString countNumAndChangeformat:[NSString stringWithFormat:@"%0.2f",wtotalF]]];
        }
    }
    
    
}
//获取点买交易时间到当前时间的天数差值
-(NSInteger)getCreatTimeFromNow
{
    
    NSDateFormatter *dateFomatter0 = [[NSDateFormatter alloc] init];
    dateFomatter0.dateFormat = @"yyyy-MM-dd HH:mm:ss";//
    NSDate *buyDate1 = [dateFomatter0 dateFromString:self.buyingTime];
    
    NSDate *nowDate = [NSDate date];
    NSDateFormatter *dateFomatter = [[NSDateFormatter alloc] init];
    dateFomatter.dateFormat = @"yyyy-MM-dd";
    NSString *beginTimeStr = [dateFomatter stringFromDate:buyDate1];
    NSString *nowDateStr = [dateFomatter stringFromDate:nowDate];
    NSDate *startDate = [dateFomatter dateFromString:beginTimeStr];
    NSDate *expireDate = [dateFomatter dateFromString:nowDateStr];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth
    | NSCalendarUnitDay;
    NSDateComponents *dateCom = [calendar components:unit fromDate:startDate toDate:expireDate options:0];
    return dateCom.day;
}



//递延费
-(NSString *)pzDeferredCharges
{
    if (!self.deferredCharges) {
        return @"--";
    }else{
        //计算一个总额度
        CGFloat totalF = [self.deferredCharges floatValue];
        if (totalF < 10000.f) {
            return [NSString stringWithFormat:@"%@元" , [NSString countNumAndChangeformat:[NSString stringWithFormat:@"%0.2f",totalF]]];
        }else{//大于万
            //转换为万
            CGFloat wtotalF = totalF / 10000.f;
            return [NSString stringWithFormat:@"%@万",[NSString countNumAndChangeformat:[NSString stringWithFormat:@"%0.2f",wtotalF]]];
        }
    }
}

-(NSString *)pzDeferredFee
{
    if (!self.deferredFee) {
        return @"--";
    }else{
        //计算一个总额度
        CGFloat totalF = [self.deferredFee floatValue];
        if (totalF < 10000.f) {
            return [NSString stringWithFormat:@"%@元" , [NSString countNumAndChangeformat:[NSString stringWithFormat:@"%0.2f",totalF]]];
        }else{//大于万
            //转换为万
            CGFloat wtotalF = totalF / 10000.f;
            return [NSString stringWithFormat:@"%@万",[NSString countNumAndChangeformat:[NSString stringWithFormat:@"%0.2f",wtotalF]]];
        }
    }
}
@end
